package com.test.cron;

import com.test.controller.MainController;
import com.test.dao.FiddlerTemplateRepository;
import com.test.entities.FiddlerTemplate;
import com.test.util.HttpUtil;
import com.test.util.SendMail;
import org.apache.commons.lang.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.quartz.impl.JobDetailImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;


public class SchedulerQuartzJob1 implements Job {

    @Autowired
    private FiddlerTemplateRepository fiddlerTemplateRepository;

    private void before() {
        System.out.println("任务开始执行");
    }

    private static final String JOB_NAME_1 = "job1";
    private static final String JOB_NAME_2 = "job2";
    private static final String GROUP_NAME_1 = "group1";
    private static final String GROUP_NAME_2 = "group2";

    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        before();
        System.out.println("开始：" + System.currentTimeMillis());
        // TODO 业务
        //new MainController().getJobData();

        //ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //HttpServletRequest request = servletRequestAttributes.getRequest();



        System.out.println("重新校验生成数据");
        String url = arg0.getJobDetail().getJobDataMap().getString("url");

        if(("127.0.0.1").indexOf(url) > -1 || ("localhost").indexOf(url) > -1)
            url.replace("8080","8000");
        String jobName = arg0.getJobDetail().getKey().getName();
        String groupName = arg0.getJobDetail().getKey().getGroup();
        if(jobName.equals(JOB_NAME_1) && groupName.equals(GROUP_NAME_1)) {
            System.out.println("sendmail to:" + url);
            HttpUtil.doGet(url + "/api/persons/testJob", "", "");
            System.out.println(JOB_NAME_1+"发送邮件");
            HttpUtil.doGet(url + "/api/persons/genReport", "", "");
            System.out.println(JOB_NAME_1+"结束：" + System.currentTimeMillis());
        } else if(jobName.equals(JOB_NAME_2) && groupName.equals(GROUP_NAME_2)){
            System.out.println(JOB_NAME_2+"发送邮件");
            HttpUtil.doGet(url + "/api/persons/excel4PerformanceTest2Mail", "", "");
            System.out.println(JOB_NAME_2+"结束：" + System.currentTimeMillis());
        }
        after();
    }

    private void after() {
        System.out.println("任务结束执行");
    }

}
